package boogui.member;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.plaf.FontUIResource;

import boogui.movie.S_MovieMain;

public class S_BoouguiMain extends JFrame {

	private KeyActionListener kal = null;
	private JPanel contentPane;
	private JTextField idTextField = null;
	private JPasswordField pwdField;
	private static S_BoouguiMain frame;
	/**
	 * Launch the application.
	 * 

	 * 
	 */
	public static void main(String[] args) {
		//System.setProperty("Quaqua.tabLayoutPolicy", "wrap");
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
				//	JFrame.setDefaultLookAndFeelDecorated(true);
				//	UIManager.setLookAndFeel(QuaquaManager.getLookAndFeel());
					frame = new S_BoouguiMain();
					
					UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
				//	UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
					frame.setIconImage(S_UISetting.initIcon());
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public S_BoouguiMain() {
		kal = new KeyActionListener();
		setTitle("부귀영화");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		S_UISetting.setUIFont(new FontUIResource("굴림", Font.PLAIN, 12));

		setSize(300, 400);
		setLocationRelativeTo(null);
		setResizable(false);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

		// 로고 패널
		JPanel logop = new JPanel();
		logop.setPreferredSize(new Dimension(10, 200));
		contentPane.add(logop, BorderLayout.NORTH);

		JLabel lblNewLabel = new JLabel("");
		lblNewLabel.setIcon(new ImageIcon(S_BoouguiMain.class.getResource("/images/sss.gif")));
	
		logop.add(lblNewLabel);

		// 로그인 패널
		JPanel loginp_1 = new JPanel();
		getContentPane().add(loginp_1, BorderLayout.CENTER);
		GridBagLayout gbl_loginp = new GridBagLayout();
		gbl_loginp.columnWidths = new int[] { 0, 0, 5, 100, 0, 0 };
		gbl_loginp.rowHeights = new int[] { 10, 0, 0, 0, 30 };
		gbl_loginp.columnWeights = new double[] { 0.0, 1.0, 0.0, 1.0, 1.0 };
		gbl_loginp.rowWeights = new double[] { 1.0, 0.0, 0.0, 0.0 };
		loginp_1.setLayout(gbl_loginp);

		JLabel idLabel = new JLabel("아  이  디");
		GridBagConstraints gbc_idLabel = new GridBagConstraints();
		gbc_idLabel.insets = new Insets(0, 0, 5, 5);
		gbc_idLabel.gridx = 1;
		gbc_idLabel.gridy = 1;
		loginp_1.add(idLabel, gbc_idLabel);

		idTextField = new JTextField();
		idTextField.setMargin(new Insets(3, 3, 3, 3));
		GridBagConstraints gbc_idTextField = new GridBagConstraints();
		gbc_idTextField.insets = new Insets(0, 0, 5, 5);
		gbc_idTextField.fill = GridBagConstraints.HORIZONTAL;
		gbc_idTextField.gridx = 3;
		gbc_idTextField.gridy = 1;
		loginp_1.add(idTextField, gbc_idTextField);
		idTextField.setColumns(10);

		JLabel pwdLabel = new JLabel("비밀번호");
		GridBagConstraints gbc_pwdLabel = new GridBagConstraints();
		gbc_pwdLabel.insets = new Insets(0, 0, 0, 5);
		gbc_pwdLabel.gridx = 1;
		gbc_pwdLabel.gridy = 3;
		loginp_1.add(pwdLabel, gbc_pwdLabel);

		pwdField = new JPasswordField();
		pwdField.addKeyListener(kal);

		pwdField.setMargin(new Insets(3, 3, 3, 3));
		GridBagConstraints gbc_pwdField = new GridBagConstraints();
		gbc_pwdField.insets = new Insets(0, 0, 0, 5);
		gbc_pwdField.fill = GridBagConstraints.HORIZONTAL;
		gbc_pwdField.gridx = 3;
		gbc_pwdField.gridy = 3;
		loginp_1.add(pwdField, gbc_pwdField);
		contentPane.add(loginp_1, BorderLayout.CENTER);

		// 부가기능 패널
		JPanel signupp = new JPanel();
		signupp.setPreferredSize(new Dimension(10, 50));

		contentPane.add(signupp, BorderLayout.SOUTH);

		JButton signupBtn = new JButton("회원가입");
		signupp.add(signupBtn);
		signupBtn.addActionListener(new BtnActionListener());
		signupBtn.setActionCommand("signup");

		JButton findBtn = new JButton("아이디/비밀번호찾기");
		findBtn.addActionListener(new BtnActionListener());
		findBtn.setActionCommand("find");
		signupp.add(findBtn);

		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.EAST);
		panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));

		JButton loginBtn = new JButton("로그인");
		loginBtn.setMaximumSize(new Dimension(70, 55));
		panel.add(loginBtn);
		loginBtn.addActionListener(new BtnActionListener());
		loginBtn.setActionCommand("login");

	}

	private class BtnActionListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			MemberVO m = new MemberVO();
			UserVO u = new UserVO();
			switch (e.getActionCommand()) {
			case "login":				
				MemberLoginService mls = new MemberLoginService();
				boolean result = false;
				String msg = "";
				u.setUserid(idTextField.getText());
				m.setMemid(idTextField.getText());
				m.setMempwd(new String(pwdField.getPassword()));

				if (m.getMemid().length() == 0) {
					msg = "아이디를 입력하세요";
				} else if (m.getMempwd().length() == 0) {
					msg = "비밀번호를 입력하세요";
				} else {

					result = mls.memberLogin(m);

					if (result) {
						MemberLoginService ms = new MemberLoginService();
						ms.getloginView(m, u);
						msg = m.getMemname() + "님 환영합니다.";
						setVisible(false);
						dispose();
						new S_MovieMain(u);
					} else {
						msg = "로그인 실패. 아이디/비밀번호를 다시 확인하세요";
						idTextField.setText(null);
						pwdField.setText(null);
						idTextField.requestFocus();
					}
				}
				JOptionPane.showMessageDialog(null, msg);

				break;
			case "signup":
				new S_SignupDialog(frame);
				break;
			case "find":
				new S_MemberFindDialog(frame);
				break;
			}
		}
	}

	private class KeyActionListener implements KeyListener {
		@Override
		public void keyPressed(KeyEvent e) {
			if(e.getKeyCode() == KeyEvent.VK_ENTER){
				MemberVO m = new MemberVO();
				UserVO u = new UserVO();
				MemberLoginService mls = new MemberLoginService();
				boolean result = false;
				String msg = "";
				u.setUserid(idTextField.getText());
				m.setMemid(idTextField.getText());
				m.setMempwd(new String(pwdField.getPassword()));

				if (m.getMemid().length() == 0) {
					msg = "아이디를 입력하세요";
				} else if (m.getMempwd().length() == 0) {
					msg = "비밀번호를 입력하세요";
				} else {
					
					result = mls.memberLogin(m);

					if (result) {
						MemberLoginService ms = new MemberLoginService();
						ms.getloginView(m, u);
						msg = m.getMemname() + "님 환영합니다.";
						setVisible(false);
						dispose();
						new S_MovieMain(u);
					} else {
						msg = "로그인 실패. 아이디/비밀번호를 다시 확인하세요";
						idTextField.setText(null);
						pwdField.setText(null);
						idTextField.requestFocus();
					}
				}
				JOptionPane.showMessageDialog(null, msg);
			}
			
		}

		@Override
		public void keyReleased(KeyEvent e) {
		}
		@Override
		public void keyTyped(KeyEvent e) {
		}

	}

}
